﻿<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">

<link rel="stylesheet" type="text/css" media="screen" href="css/smoothness/jquery-ui-1.10.3.custom.min.css" />	<!-- jqueryUI CSS -->
<link rel="stylesheet" type="text/css" media="screen" href="css/selfTemplate.css" />						<!-- 本專案自行定義 CSS -->
<script src="js/jquery-1.9.1.min.js"></script>		<!-- jQuery 核心 -->
<script src="js/jquery-ui-1.10.3.custom.min.js" type="text/javascript"></script>							<!-- jqueryUI 核心 -->
<script src="js/jquery.cookie.js"></script>																	<!-- 操作cookie用 -->
<script src="js/widgets.js" type="text/javascript"></script>												<!-- 本專案自行定義 JS -->

<title>部署執行 </title>

<script type="text/javascript">

var defWidth="670";
var chgWidth="";		//驗證完主機後，要改變畫面寬度
var tmp = "";
var tmpDeliverySN = "";	//輸出交付代碼
var tmpDeplmainSN = "";	//輸出部署計畫代碼
var tmpVer = "";		//輸出檔案版次
var fl = [];
var depPath="";
var svnPath="";
var dialogEffect = "slide";


function reDepInit(type,subSysId,envId) {
	
	$("#reDepConfirm"+type+" td#cons").remove();
	
	/*	產生子系統下拉供選擇 */
	$.post("QuerySubSys",{ fc:"revDep",loginUser: $.cookie("userId"),isSU:$.cookie("isSU") },function(reses) {
		if (reses.length > 0 ) {
			$("#reDepConfirm"+type+" select#selSub option").remove();
			$.each(reses,function(i, res) {
				$("#reDepConfirm"+type+" select#selSub").append("<option value='"+res.id+"'>"+res.id+" ("+res.name+")</option>");
				if (subSysId == res.id) {
					$("#reDepConfirm"+type+" select#selSub option:last").attr("SELECTED","SELECTED");
				}
			});				
		}
	},"JSON");
	
	/*	產生環境下拉供選擇 */
	$.post("QueryEnv",{fc:"selEnvBySubSys",subSysId:subSysId},function(reses) {
		if (reses.length > 0) {
			$("#reDepConfirm"+type+" select#selEnv option").remove();
			$.each(reses,function(i, res) {
				if (envId == res.id) { selected="SELECTED='SELECTED'"; } else { selected=""; }
				$("#reDepConfirm"+type+" select#selEnv").append("<option "+selected+">"+res.id+" ("+res.name+")</option>");
			});
		}
	},"JSON");
}


function validate(type) {
	if ( $("#reDepConfirm"+type+" textarea").val() == "" ) {
		alert("部署原因必須輸入");
	} else if  ( type=="Single" && $("#reDepConfirm"+type+" ul li").size() ==0 )  {
		alert("資源群組必須選擇");
	} else {
		subSysId = $("#reDepConfirm"+type+" select#selSub option:checked").val();
		/* 這邊應該要用 .val() 才對，但不work，故用這種方式 <start>*/
		tmp = $("#reDepConfirm"+type+" select#selEnv option:checked").html();
		envId = tmp.substring( 0, tmp.indexOf('(') ); 
		/* 這邊應該要用 .val() 才對，但不work，故用這種方式 <end>*/
		
		var tmp2 =  "";
		v_tar = "";
		$("#reDepConfirm"+type+" td#cons").remove();
		var filePortfolio= "";
		if ( type=="Single" ) {
			$("#filePortFolio").val(null);		//先清空
			sq = [];
			tmp = "";
			sqid = "";
			v_tar = "";	//資源群組，組合成字串，供驗證的ValidateHost使用
			$("#reDepConfirm"+type+" ul li").each(function(r,level3){
				// 檔案 >> **資源群組
				tmp = level3.innerHTML;
				sqid = tmp.substring(tmp.indexOf('(')+1,tmp.length-1);
				sqname = tmp.substring(0,tmp.indexOf('('));
				sqpath = "DUMMY";
				sq.push({sqid:sqid,sqname:sqname,sqpath:sqpath});
				v_tar += (splitTokenONE+sqid);
			});
			v_tar = v_tar.substring(splitTokenONE.length,v_tar.length);
			fl = [];
			fl.push( {
				fileName:$("#reDepConfirm"+type+" td:first").html(),
				ver:$("#reDepConfirm"+type+" select:first option:selected").html(),
				depType:$("#reDepConfirm"+type+" select#selDepType option:selected").html(),
				depPath:$("#reDepConfirm"+type+" input[type='text']").val(),
				svnPath:svnPath,
				sq:sq
			});
			filePortfolio= maniFilePortfolio(  $("#reDepConfirm"+type+" select#selDepType option:selected").html() );
			
			tmp2=" rowspan='9'";
			tmp = "first";
			chgWidth="860";
		} else {
			v_tar = $("#reDepConfirm"+type+" td:first").html();	//部署計劃代碼
			if (v_tar.indexOf('(') != -1 ) {
				v_tar = v_tar.substring(0,v_tar.indexOf('('));	
			}
			filePortfolio= $("button.reDepALL[deplmainSN='"+v_tar+"']").attr("filePortfolio" );	//取得該批次重新部署上的檔案部署組合值
			tmp = "last";
			tmp2=" colspan='2'";
			chgWidth="380";
		}
		$("#reDepConfirm"+type+" tr:"+tmp).append("<td id='cons' class='top' "+tmp2+"><label for=\"validate\" class=\"eyecatch\"> 確認設定中...</label><br/><img id='validate' src='pic/ajax-loader.gif'></td>");
	    $(".ui-dialog-buttonpane button:contains('驗證主機')").button("disable");
		
		$.post("ValidateHost",{bssId:"MBMS",subSysId:subSysId,envId:envId,v_tar:v_tar,type:type,filePortfolio:filePortfolio  }, function(reses) {
			$("#reDepConfirm"+type+" td#cons").remove();
			$("#reDepConfirm"+type+" tr:"+tmp).append("<td id='cons' class='top'"+tmp2+"></td>");
			if ( reses["web"].isOK && reses["nonWeb"].res == "1111" ) {
				tmp = "確認主機設定正常";
				$("#reDepConfirm"+type+" #cons").append(tmp);
				$("#reDepConfirm"+type+"").dialog("option", "buttons", {
					"執行" : function() {
						$(".ui-dialog-buttonpane button:contains('執行')").button("disable");
						
						if ( type=="Single" ) {
							fileName = $("#reDepConfirm"+type+" td:first").html();
							ver = $("#reDepConfirm"+type+" select:first option:selected").html();
							filePath = $("#reDepConfirm"+type+" input[type='text']").val();
							tmp = {type:type,bssId:"MBMS",subSysId:subSysId,envId:envId,fileName:fileName,ver:ver,filePath:filePath,hsLists:JSON.stringify(reses["nonWeb"].hsList),webSQ:reses["web"].mes,filePortfolio:filePortfolio }; 
						} else {
							v_tar = $("#reDepConfirm"+type+" td:first").html();	//部署計劃代碼
							if ( v_tar.indexOf('(') != -1 ) {
								v_tar = v_tar.substring( 0, v_tar.indexOf('(') );	
							}
							tmp2 = $("#reDepConfirmBatch #depmainSN").val();
							tmp = { type:type, bssId:"MBMS",subSysId:subSysId,envId:envId,deplmainSN:v_tar, depmainSN:tmp2, filePortfolio:filePortfolio };
						}
						
						
						$.post("ValidateHistory",tmp, function(reses2) {
							$("#confirmDep").html("");
							
							/*	如果 web或nonweb的結構反應不OK，便要執行web	*/
							if ( !reses2.isNonWebOK  || !reses2.isWebOK ) {
								if ( type == "Single" ) {
									tmp = "部署的主機路徑上，已存在更新的檔案!<ul id='ulc'></ul>";
									$("#confirmDep").append(tmp);
								}
								
								if ( !reses2.isNonWebOK ) {
									$.each(reses2["nonWeb"],function(i, res) {
										if ( res.fileResult == "FAIL" ) {	//表示此檔案，這在次部署的主機群中，有一台以上的主機，其版本更新
											if ( type=="Batch" ) {
												if ( $("#tabs").length == 0 ) {
													tmp = "<p class='alert'>部署的主機路徑上，已存在更新版本的檔案!<br>確定要執行部署？</p><p>下列各分頁顯示詳細資訊。<div id=\"tabs\"><ul id='one'></ul></div>";
													$("#confirmDep").append(tmp);
												}
												if ( $("div#tabs-"+res.depldetailid ).length == 0 ) {
													tmp = "<li><a href=\"#tabs-"+res.depldetailid+"\">"+res.depldetailid+". "+res.fileName+"<br>( "+res.fileVer+" ) "+res.filePath+"</a></li>";
													$("#confirmDep #tabs ul#one").append(tmp); 
													tmp = "<div id='tabs-"+res.depldetailid+"'><ul id='"+res.depldetailid+"'></ul></div>";
													$("#confirmDep #tabs").append(tmp);	
												}
											}
											
											
											$.each( res.vhses, function(i, resA) {
												if ( resA.squadResult == "FAIL" ) {		//表示此資源群組上，被部署過更新的版本
													if ( type == "Single") {
														chgWidth=860;
														$("#confirmDep ul#ulc").append("<li>"+resA.squadName+" ("+resA.squadId+")<ul id='ul"+resA.squadId+"'></ul></li>");
													} else {
														chgWidth=590;
														$("div#tabs ul#"+res.depldetailid).append("<li>"+resA.squadName+" ("+resA.squadId+")<ul id='"+resA.squadId+"'></ul></li>");
													}
													$.each(resA.laos,function(i, res2) {
														
														if ( res2.e != undefined) {
															if (type == "Single") {
																tmp = "<li>"+res2.a+" ("+res2.b+")<br>"+res2.c+"於"+res2.d+"部署"+res2.e+"版</li>";
																$("#confirmDep ul#ulc ul#ul"+resA.squadId).append(tmp);	
															} else {
																tmp = "<li>"+res2.a+" ("+res2.b+")<br>"+res2.c+"於"+res2.d+"部署"+res2.e+"版</li>";
																$("div#tabs ul#"+res.depldetailid+" ul#"+resA.squadId).append(tmp);
																if ( $("div#tabs ul#"+res.depldetailid+" ul#"+resA.squadId ).length > 0 ) {
																		
																}
															}
														}
													});	
												}
											});
										}
									});		// end of each
								}
								
								if ( !reses2.isWebOK ) {
									$.each( reses2["web"], function(i, res) {
										$.each( res.vhses, function(i, resA) {
											if ( resA.squadResult == "FAIL" ) {		//表示此資源群組上，被部署過更新的版本
												if ( type=="Batch" ) {
													if ( $("#tabs").length == 0 ) {
														tmp = "<p class='alert'>部署的主機路徑上，已存在更新版本的檔案!<br>確定要執行部署？</p><p>下列各分頁顯示詳細資訊。<div id=\"tabs\"><ul id='one'></ul></div>";
														$("#confirmDep").append(tmp);
													}
													if ( $("div#tabs-"+res.depldetailid ).length == 0 ) {
														tmp = "<li><a href=\"#tabs-"+res.depldetailid+"\">"+res.depldetailid+". "+res.fileName+"<br>( "+res.fileVer+" ) "+res.filePath+"</a></li>";
														$("#confirmDep #tabs ul#one").append(tmp); 
														tmp = "<div id='tabs-"+res.depldetailid+"'><ul id='"+res.depldetailid+"'></ul></div>";
														$("#confirmDep #tabs").append(tmp);	
													}
												}
												
												if ( type == "Single") {
													chgWidth=860;
													$("#confirmDep ul#ulc").append("<li>"+resA.squadName+" ("+resA.squadId+")<ul id='ul"+resA.squadId+"'></ul></li>");
												} else {
													chgWidth=590;
													$("div#tabs ul#"+res.depldetailid).append("<li>"+resA.squadName+" ("+resA.squadId+")<ul id='"+resA.squadId+"'></ul></li>");
												}
												$.each(resA.laos,function(i, res2) {
													if ( res2.e != undefined) {
														if (type == "Single") {
															tmp = "<li> Web主機  "+res2.a+"<br>"+res2.c+"於"+res2.d+"部署"+res2.e+"版</li>";
															$("#confirmDep ul#ulc ul#ul"+resA.squadId).append(tmp);	
														} else {
															tmp = "<li> Web主機  "+res2.a+"<br>"+res2.c+"於"+res2.d+"部署"+res2.e+"版</li>";
															$("div#tabs ul#"+res.depldetailid+" ul#"+resA.squadId).append(tmp);
															if ( $("div#tabs ul#"+res.depldetailid+" ul#"+resA.squadId ).length > 0 ) {
															}
														}
													}
												});	
											}
										});
									});
								}
								
								if ( type=="Batch") {
									$("#tabs").tabs();
								}
								
								/*	不管如何都要被執行	*/ 
								$("#confirmDep").css("visibility", "visible").dialog({
									width: chgWidth,
									modal : true,
									show: dialogEffect,
									hide: dialogEffect,
									buttons : {
										"確定" : function() {
											openLoading("部署進行中...");
											if ( type == "Single") {
												tmp = {	
													subSysId:subSysId,
													envId:$.trim(envId),
													sqList:JSON.stringify(sq),
													fileName:$("#reDepConfirm"+type+" td:first").html(),										//檔案名稱														
													ver:$("#reDepConfirm"+type+" select:first option:selected").html(),				//版次
													depPath:$("#reDepConfirm"+type+" input[type='text']").val(),						//延伸路徑	
													svnPath:svnPath,																										//svn上的路徑
													comment:$("#reDepConfirm"+type+" textarea").val(),										//送出的comment
													loginUser:$.cookie("userId"),																					//人員ID
													filePortfolio:$("#filePortfolio").val()
												}	//送出的那些參數
												
												$.post("RedepBySingleFile", tmp, function(reses) {
													alert(reses);
												}); 	
											} else if (type == "Batch") {
												v_tar = $("#reDepConfirm"+type+" td:first").html();	//部署計劃代碼
												if (v_tar.indexOf('(') != -1 ) {
													v_tar = v_tar.substring(0,v_tar.indexOf('('));	
												}
												/*	有更新版本時	*/
												tmp = {
													comment:$("#reDepConfirm"+type+" textarea").val(),
													subSysId:subSysId,
													deplmainSN:v_tar,
													envId:$.trim(envId),
													loginUser:$.cookie("userId"),
													isByDel:false,
													isReDep:true,
													filePortfolio:$("#filePortfolio").val() 
												};
												
												$.post("BeforeDep",tmp,function(reses) {
													alert(reses);
												}); 
											}
											
											closeLoading();
											$(this).dialog("close");
										},"關閉" : function() {
											$(this).dialog("close");
										}
									}
								});
							} else {		//目前部署的版次便是最新的，直接進行部署
								if ( type == "Single" ) {
									tmp = {	
										subSysId:subSysId,
										envId:$.trim(envId),
										sqList:JSON.stringify(sq),
										fileName:$("#reDepConfirm"+type+" td:first").html(),										//檔案名稱														
										ver:$("#reDepConfirm"+type+" select:first option:selected").html(),				//版次
										depPath:$("#reDepConfirm"+type+" input[type='text']").val(),						//延伸路徑	
										svnPath:svnPath,																										//svn上的路徑
										comment:$("#reDepConfirm"+type+" textarea").val(),										//送出的comment
										loginUser:$.cookie("userId"),																					//人員ID
										filePortfolio:$("#filePortfolio").val()																		
									}	//送出的那些參數
									
									//目前部署的版次便是最新的，直接進行部署
									openLoading("部署進行中...");
									$.post("RedepBySingleFile", tmp, function(reses) {
										alert(reses);
									});									
								} else if ( type=="Batch" ) {
									$("#tabs").tabs();
									v_tar = $("#reDepConfirm"+type+" td:first").html();	//部署計劃代碼
									if (v_tar.indexOf('(') != -1 ) {
										v_tar = v_tar.substring(0,v_tar.indexOf('('));	
									}
									tmp = {comment:$("#reDepConfirm"+type+" textarea").val(),subSysId:subSysId,deplmainSN:v_tar,envId:$.trim(envId),loginUser:$.cookie("userId"),isByDel:false,isReDep:true,filePortfolio:$("#filePortfolio").val() };
									//目前部署的版次便是最新的，直接進行部署
									$.post("BeforeDep", tmp,function(reses) {
										alert(reses);
									});									
								}
								closeLoading();
							}
						},"JSON");
					},"關閉" : function() {
						$(this).dialog("option","width", defWidth ).dialog("close");
					}				
				});
			} else {
				if ( !reses["web"].isOK ) {
					tmp = res["web"].mes;
					$("#reDepConfirm"+type+" #cons").append("環境設定產生問題：<br>"+tmp);
				}
				
				var result = reses["nonWeb"].res;
				if ( result.charAt(0) == "0" ) {
					tmp = "下列資源群組並未設定對應主機<ul id='ula'></ul>";
					$("#reDepConfirm"+type+" #cons").append(tmp);
					$.each( reses["nonWeb"].squadsWithNoHost,function(i, res) {
						tmp = ("<li>"+res.b+" ("+res.a+" )</li>");
						$("#reDepConfirm"+type+" #cons ul#ula").append(tmp);
					});
				}
				if ( result.charAt(1) == "0" ) {
					tmp = "下列資源群組的主機無法登入<ul id='ulb'></ul>";
					$("#reDepConfirm"+type+" #cons").append(tmp);
					$.each( reses["nonWeb"].v2, function(i, res2) {
						tmp = ("<li><ul id='"+res2.res+"'>"+res2.squadName+" ("+res2.res+" )</ul></li>");
						$("#reDepConfirm"+type+" #cons ul#ulb").append(tmp);
						$.each(res2.hostCantLogin,function(j, res3) {
							tmp = ("<li>"+res3.a+" ( "+res3.b+" ) -"+res3.c+"</li>");
							$("#reDepConfirm"+type+" #cons ul#ulb ul#"+res2.res).append(tmp);
						});
					});
				}
				if ( result.charAt(2) == "0" ) {
					tmp = "下列資源群組存在不存在的部署絡徑<ul id='ulc'></ul>";
					$("#reDepConfirm"+type+" #cons").append(tmp);
					$.each( reses["nonWeb"].v2 ,function( i, res2 ) {
						tmp = ("<li><ul id='"+res2.res+"'>"+res2.squadName+" ("+res2.res+" )</ul></li>");
						$("#reDepConfirm"+type+" #cons ul#ulc").append(tmp);
						$.each(res2.pathNotExist, function(j, res3) {
							tmp = ("<li>"+res3.a+" ( "+res3.b+" ) -"+res3.c+"</li>");
							$("#reDepConfirm"+type+" #cons ul#ulc ul#"+res2.res).append(tmp);
						});
					});
				}
				if ( result.charAt(3) == "0" ) {
					tmp = "下列資源群組存在的部署絡徑無法存取<ul id='uld'></ul>";
					$("#reDepConfirm"+type+" #cons").append(tmp);
					$.each( res["nonWeb"].v2 ,function( i, res2 ) {
						tmp = ("<li><ul id='"+res2.res+"'>"+res2.squadName+" ("+res2.res+" )</ul></li>");
						$("#reDepConfirm"+type+" #cons ul#uld").append(tmp);
						$.each(res2.pathCantAccess, function(j, res3) {
							tmp = ("<li>"+res3.a+" ( "+res3.b+" ) -"+res3.c+"</li>");
							$("#reDepConfirm"+type+" #cons ul#uld ul#"+res2.res).append(tmp);
						});
					});
				}
				
				
				$("#reDepConfirm"+type+"").dialog("option","width", chgWidth );
				tmp = $(".ui-dialog-buttonpane button:contains('驗證主機')");
			    $(tmp).button("enable");
			}
			
		},"JSON");
		
	}	
}


$(document).ready(function() {
	
	if ($.cookie("userId") == null) {
		alert("請先登入系統");
		location.assign("index.html");
	} else {
		$("#q_form input").val("");
		
		$("input.date").prop("size","13").prop("readonly",true).datepicker({ dateFormat:"yy-mm-dd" });
		
		/*	日期設定 */
		var myDate = new Date();
		myDate.setDate(myDate.getDate() - $.cookie("QueryDateRange") );
		$("#q_fromDate").datepicker('setDate', myDate);
		$("#q_form table td#mes").html("※ 預設查詢近"+$.cookie("QueryDateRange")+"天記錄");
		
		/*	產生子系統下拉供選擇 */
		$.post("QuerySubSys",{fc:"revDep",loginUser: $.cookie("userId"),isSU:$.cookie("isSU") },function(reses) {
			if (reses.length > 0 ) {
				$.each(reses,function(i, res) {
					$("#envSubSysId").append("<option value='"+res.id+"'>"+res.name+" ("+res.id+")</option>");
				});				
			}
		},"JSON");
		
		/*	選擇了子系統 */
		$("#envSubSysId").on("change",function(){
			if ( $("#envSubSysId option:checked").val() != "請選擇" ) {
				subSysId =  $("#envSubSysId option:checked").val()
				$.post("QueryEnv",{fc:"selEnvBySubSys",subSysId:subSysId},function(reses) {
					if (reses.length > 0 ) {
						$("#envId option").remove();
						$("#envId").append("<option value=\"\">請選擇</option>");
						$.each(reses,function(i, res) {
							$("#envId").append("<option value='"+res.id+"'>"+res.name+" ("+res.id+")</option>");
						});				
					}
				},"JSON");
			} else {
				$("#envId option").remove();
				$("#envId").append("<option value=\"\">請選擇</option>");
			}
		});
		
		
		
		$(".dialog").css("visibility","hidden");
		
		$(".sn").prop("size","4").prop("maxlength","5").bind("keyup",function(){
			return validateNumber(this,this.value); 
		});
		
		
		$("#q_performer").prop("readonly",true).bind("click",function(){
			if ($("#performerList tr").size() == 0 ) {
				$.post("QueryMailList",function(reses) {
					
					$.each(reses,function(i, res) {
						tmp = "<tr><td><input type='radio' value='"+res.id+"' name='qpn'/></td>"+	//CheckBox
								"<td class='english_font center center' >"+(i+1)+"</td>"+			//序號
								"<td class='center'>"+res.name+"</td>"+								//姓名
							"</tr>"
						$("#performerList").append(tmp);
					});
					refreshUI("performerList",reses.size,false);
					$("#ptrs").val( $("#performerList tr").size() );
				},"JSON");	
			}
			
			$("#perfromerPicker").css("visibility", "visible").dialog({
				resizable : true,
				modal : true,
				width : 160,
				height : 300,
				show: dialogEffect,
				hide: dialogEffect,
				buttons : {
					"關閉" : function() {
						$("#q_performer").val($("#performerList input[type='radio']:checked").parent().next().next().html());
						$("#q_performerId").val($("#performerList input[type='radio']:checked").val());
						$(this).dialog("close");
					}
				}
			});
		});
		
		$("#clearBut").bind("click",function(){
			$("#q_form input").val("");
			$("#forRemoveQueryRes").remove();
		}).button();
		 
		
		/*		按下 "送出"鈕		*/
		$("#submitQueryBut").bind("click",function(){

			$("#forRemoveQueryRes").remove();
			tmp = "<div id='forRemoveQueryRes'><p><label for='imgLoading'> 查詢中...</label><br/><img id='imgLoading' src='pic/ajax-loader.gif'></div>"; 
			$("#queryRes").append(tmp);
			
			$("#q_envSubSysId").val($("#envSubSysId option:checked").val());
			$("#q_envId").val($("#envId option:checked").val());
			$("#q_form #isSU").val( $.cookie("isSU") );
			$("#q_form #userId").val( $.cookie("userId") );
			$.post("QueryDep",$("#q_form").serializeArray(),function(reses) {
				if ( reses.length > 0 ) {
					
					$("#forRemoveQueryRes").remove();
					tmp="<div id='forRemoveQueryRes'><p><p><hr><p><table><tr><th>&nbsp;</th><th>部署序號</th><th>部署計畫代碼</th><th>交付代碼</th><th>執行人員</th><th>部署日期</th><th>子系統</th><th>部署環境</th><th colspan='2'>部署細項</th><th>程式名稱</th><th>部署版次</th><th>執行方式</th><th>資源群組</th></tr><tbody id='queryResBody'></tbody></table></div>";
					$("#queryRes").append(tmp);
					
					var preDepmainSN = 0;
					var preDepmainID = 0;
					var cc="";
					var count = 1;
					$.each(reses,function(i, res) {
						subSystdId = res.depmainSN+"_"+res.depdetailid+"_"+res.squadSN+"_subsys";
						squadtdId = res.depmainSN+"_"+res.depdetailid+"_"+res.squadSN+"_squad";
						
						if ( preDepmainSN != res.depmainSN ) {
							$("#filePortfolio").val(null);
							count=1;	//歸零
						} else {
							count++;
						}
						
						if ((res.depmainSN%2) ==0) {
							tpe="oven";
						} else {
							tpe="odd";
						}
						
						if (res.deliverySN==0) {
							tmpDeliverySN = "手動部署";
							tmpDeplmainSN = res.deplmainSN;
							tmpVer = res.localVer;
							cc = "class='"+tpe+"'";
						} else {
							tmpDeliverySN = res.deliverySN+"("+res.deliveryTarSN+")";
							tmpDeplmainSN = res.deplmainSN+"("+res.deplmainTarSN+")";
							tmpVer = res.localVer+"("+res.fromVer+")";
							cc = "class='english_font right "+tpe+"'";
						}
						tmpDepType =  Boolean(res.isNonWeb)?"non-web":"web";
						
						
						if ( res.sizeNum!=1 ) {
							if (count==1) {	//那次部署的第一筆
								if ( res.squadNum ==1) {
									num = res.sizeNum+1;
								} else {
									num = res.sizeNum;
								}
								tmp = "<tr class='"+tpe+"'>"+
								"<td rowspan='"+num+"' class='center "+tpe+"'><button type='button' class='english_font reDepALL batch' envId='"+res.envId+"' subSysId='"+res.subsysid+"'  filePortfolio='"+$("#filePortfolio").val()+"' deliverySN='"+res.deliverySN+"' depmainSN='"+res.depmainSN+"' deplmainSN='"+res.deplmainSN+ "' deplComment='"+res.deplComment+"' >B</button></td>"+
								"<td rowspan='"+num+"' class='english_font center "+tpe+"'>"+res.depmainSN+"</td>"+												//部署記錄代碼
								"<td rowspan='"+num+"' class='english_font right "+tpe+"'>"+tmpDeplmainSN+"</td>"+												//部署計畫代碼
								"<td rowspan='"+num+"' "+cc+">"+tmpDeliverySN+"</td>"+																									//交易代碼
								"<td class='"+tpe+"' rowspan='"+num+"' >"+res.username+"</td>"+																						//執行人員
								"<td class='english_font "+tpe+"' rowspan='"+num+"'>"+res.createdDateTime+"</td>"+													//部署日期
								"<td id='"+subSystdId+"' class='"+tpe+"' rowspan='"+num+"'>"+res.subsysName+"("+res.subsysid+")"+"</td>"+	//子系統
								"<td class='"+tpe+"' rowspan='"+num+"'>"+res.envName+" ("+res.envId+")</td>";															//環境
								if ( res.squadNum ==1) {
									tmp += "</tr>"; 
								} else{
									tmp += "<td rowspan='"+res.squadNum+"' class='"+tpe+"'><button class='reDep alert' envId='"+res.envId+"' subSysId='"+res.subsysid+"' localPath='"+res.localPath+"' depmainSN='"+res.depmainSN+"' depPath='"+res.depPath+ "' depType='"+tmpDepType+ "' deplComment='"+res.deplComment+"' type='button'>b</button></td>"+
									"<td rowspan='"+res.squadNum+"' class='english_font right "+tpe+"' >"+res.depdetailid+"</td>"+							//部署執行細項
									"<td rowspan='"+res.squadNum+"' class='english_font "+tpe+"'>"+res.localFile+"</td>"+											//程式名稱
									"<td rowspan='"+res.squadNum+"' class='english_font right "+tpe+"'>"+tmpVer+"</td>"+											//部署版次
									"<td rowspan='"+res.squadNum+"' class='english_font right "+tpe+"'>"+tmpDepType+"</td>"+								//執行方式
									"<td id='"+squadtdId+"' class='"+tpe+"'>"+res.squadName+" ("+res.squadid+")"+"</td>"+											//資源群組
									"</tr>" 										
								}
								$("#queryResBody").append(tmp);
								
								if ( res.squadNum ==1) {
									tmp = "<tr class='"+tpe+"'>"+
									"<td class=' "+tpe+"' ><button class='reDep alert' envId='"+res.envId+"' subSysId='"+res.subsysid+"' localPath='"+res.localPath+"' depmainSN='"+res.depmainSN+"' depType='"+tmpDepType+ "' depPath='"+res.depPath+ "' deplComment='"+res.deplComment+"' type='button'>b</button></td>"+
									"<td class='english_font right "+tpe+"' >"+res.depdetailid+"</td>"+															//部署執行細項
									"<td class='english_font "+tpe+"'>"+res.localFile+"</td>"+																			//程式名稱
									"<td class='english_font right "+tpe+"'>"+tmpVer+"</td>"+																			//部署版次
									"<td class='english_font right "+tpe+"'>"+tmpDepType+"</td>"+																//執行方式
									"<td id='"+squadtdId+"' class='"+tpe+"'>"+res.squadName+" ("+res.squadid+")"+"</td>"+					//資源群組
									"</tr>";
									$("#queryResBody").append(tmp);		
								}
							} else { 	//非那次部署的第一筆
								if (res.depdetailid == preDepmainID ) { //同個項次，不同squad
									tmp = "<tr class='"+tpe+"'>"+
									"<td id='"+squadtdId+"' class='"+tpe+"'>"+res.squadName+" ("+res.squadid+")"+"</td>"+					//資源群組
									"</tr>";
								} else {	//不同項次
									tmp = "<tr class='"+tpe+"'><td rowspan='"+res.squadNum+"' class=' "+tpe+"' ><button class='reDep alert' envId='"+res.envId+"' subSysId='"+res.subsysid+"' localPath='"+res.localPath+"' depmainSN='"+res.depmainSN+"' depType='"+tmpDepType+ "' depPath='"+res.depPath+ "' deplComment='"+res.deplComment+"' type='button'>b</button></td>"+
									"<td rowspan='"+res.squadNum+"' class='english_font right "+tpe+"' >"+res.depdetailid+"</td>"+	//部署執行細項
									"<td rowspan='"+res.squadNum+"' class='english_font "+tpe+"'>"+res.localFile+"</td>"+					//程式名稱
									"<td rowspan='"+res.squadNum+"' class='english_font right "+tpe+"'>"+tmpVer+"</td>"+					//部署版次
									"<td rowspan='"+res.squadNum+"' class='english_font right "+tpe+"'>"+tmpDepType+"</td>"+		//執行方式
									"<td id='"+squadtdId+"' class='"+tpe+"'>"+res.squadName+" ("+res.squadid+")"+"</td>"+					//資源群組
									"</tr>";
								}
								$("#queryResBody").append(tmp);
							}
							
						} else {
							tmp = "<tr class='"+tpe+"'>"+
							"<td class='center "+tpe+"'><button type='button' class='english_font reDepALL batch' envId='"+res.envId+"' subSysId='"+res.subsysid+"' deliverySN='"+res.deliverySN+"' depmainSN='"+res.depmainSN+"' deplmainSN='"+res.deplmainSN+"' deplComment='"+res.deplComment+"' filePortfolio='"+$("#filePortfolio").val()+"' >B</button></td>"+
							"<td rowspan='1' class='english_font center "+tpe+"'>"+res.depmainSN+"</td>"+											//部署記錄代碼
							"<td class='english_font right "+tpe+"'>"+tmpDeplmainSN+"</td>"+																	//部署計畫代碼
							"<td "+cc+">"+tmpDeliverySN+"</td>"+																														//交易代碼
							"<td class='"+tpe+"'>"+res.username+"</td>"+																										//執行人員
							"<td class='english_font "+tpe+"'>"+res.createdDateTime+"</td>"+																	//部署日期
							"<td id='"+subSystdId+"' class='"+tpe+"'>"+res.subsysName+"("+res.subsysid+")"+"</td>"+						//子系統
							"<td class='"+tpe+"'>"+res.envName+" ("+res.envId+")</td>"+																			//環境
							"<td class=' "+tpe+"' ><button class='reDep alert' envId='"+res.envId+"' subSysId='"+res.subsysid+"' localPath='"+res.localPath+"' depmainSN='"+res.depmainSN+"' depPath='"+res.depPath+ "' depType='"+tmpDepType+ "' deplComment='"+res.deplComment+"' type='button'>b</button></td>"+
							"<td class='english_font right "+tpe+"' >"+res.depdetailid+"</td>"+																	//部署執行細項
							"<td class='english_font "+tpe+"'>"+res.localFile+"</td>"+																					//程式名稱
							"<td class='english_font right "+tpe+"'>"+tmpVer+"</td>"+																					//部署版次
							"<td class='english_font right "+tpe+"'>"+tmpDepType+"</td>"+																		//執行方式
							"<td id='"+squadtdId+"' class='"+tpe+"'>"+res.squadName+" ("+res.squadid+")"+"</td>"+							//資源群組
							"</tr>";
							$("#queryResBody").append(tmp);
							
						}
						
						$("button.reDepALL[deplmainSN='"+res.deplmainSN+"']").attr("filePortfolio", maniFilePortfolio( tmpDepType ) );
						
						preDepmainSN = res.depmainSN;
						preDepmainID = res.depdetailid;
					});
					
					$(".reDepALL").button().bind("click",function() {
						subSysId = $(this).attr("subSysId");		//取得  子系統
						envId = $(this).attr("envId");				//取得  環境
						 
						$("#reDepConfirmBatch #depmainSN").val( $(this).attr("depmainSN") );	//部署序號 當執行批 重新部署時，直接執行當時序號所執行的內容				
						$("#reDepConfirmBatch td:first").html( $(this).parent().next().next().html() );
						$("#reDepConfirmBatch textarea").val( $(this).attr("deplComment")=="undefined"?"":$(this).attr("deplComment") );
						
						reDepInit("Batch",subSysId,envId);
						
						$("#reDepConfirmBatch").prop("title","").css("visibility", "visible").dialog({
							width : 370,
							modal : true,
							show: dialogEffect,
							hide: dialogEffect,
							buttons : {
								"驗證主機" : function() {
									validate("Batch");
								},"關閉" : function() {
									$(this).dialog("close"); }
							}
						}).prev().hide();	
					});
					
					$(".reDep").button().bind("click", function(){
						openLoading("取得檔案SVN中簽入歷程...");
						
						subSysId = $(this).attr("subSysId");		//取得  子系統
						envId = $(this).attr("envId");				//取得  環境
						$("#reDepConfirmSingle textarea").val( $(this).attr("deplComment")=="undefined"?"":$(this).attr("deplComment") );
						
						$("#reDepConfirmSingle td:first").html( $(this).parent().next().next().html() );		//檔名
						$("#reDepConfirmSingle input[type='text']").val($(this).attr("depPath"));				//延伸路徑
						$("#reDepConfirmSingle textarea").val("");																	//清除註解
						squadNum = $(this).parent().attr("rowspan")==undefined?1:$(this).parent().attr("rowspan"); //這支程式的Squad數
						$("#reDepConfirmSingle ul li").remove();
						
						reDepInit("Single",subSysId,envId);						
						
						
						$("#reDepConfirmSingle button").bind("click",function(){
							selDepConf(this,'2','n');
							$("#reDepConfirmSingle").dialog("option", "buttons", {
								"驗證主機" : function() {
									validate("Single");
								},"關閉" : function() { $("#reDepConfirmSingle").dialog("option","width", defWidth ).dialog("close"); }				
							}).prev().hide();
							$("#reDepConfirmSingle td#cons").remove();
							$("#reDepConfirmSingle").dialog( "option", "width", defWidth );
						});
						
						
						for (i=0;i<squadNum;i++) {
							squad = $("#"+ $(this).attr("depmainSN")+"_"+ $(this).parent().next().html()+"_"+(i+1)+"_squad" ).html();
							$("#reDepConfirmSingle ul").append("<li>"+squad+"</li>");
						}
						
						
						fileVer = $(this).parent().next().next().next().html();
						if ( fileVer.indexOf('(') != -1 ) {
							fileVer = fileVer.substring(0,fileVer.indexOf('('));
						}

						fileName = $(this).parent().next().next().html();
						var dt = $(this).attr("depType");
						/*	產生執行方式的下接選單	*/
					 	$("select#selDepType option").remove(); 		//先將舊的移除
						$("select#selDepType").append("<option value='n' >non-web</option>");
						
						if ( fileName.indexOf(".war") != -1 || fileName.indexOf(".ear") != -1 ) {
							 if ( dt.indexOf("non") != -1 ) {
								$("select#selDepType").append("<option value='w' >web</option>");
								$("#depPath input:first").attr("disabled", false);
							} else {
								$("select#selDepType").append("<option value='w' SELECTED >web</option>");
								$("#depPath input:first").attr("disabled", true);
							}
						}
						
						
						svnPath = $(this).attr("localpath");
						$.post("QuerySVN",{ svsnPath:$(this).attr("localpath"), fn:fileName,subSysId:$(this).attr("subSysId") },function(reses) {
							if (reses.length > 0) {
								$("#reDepConfirmSingle select:first option").remove();
								
								$.each(reses,function(i, res) {
									$.each(res.verSet,function(i, ver) {
										selected="";
										if ( ver== fileVer ) { selected="SELECTED='SELECTED'"; }
										$("#reDepConfirmSingle select:first").append("<option "+selected+">"+ver+"</option>");
									});
								});
								
								closeLoading();
								/* 取得Button中的屬性，等入BeforeDep要用 */
								
								
								$("#reDepConfirmSingle").prop("title","部署").css("visibility", "visible").dialog({
									width : defWidth,
									modal : true,
									show: dialogEffect,
									hide: dialogEffect,
									buttons : {
										"驗證主機" : function() {
											validate("Single");
										},	"關閉" : function() { $(this).dialog("option","width", defWidth ).dialog("close"); }
									}
								});
							} else {
								closeLoading();
								alert("檔案已不存在SVN中!!");
							}
						},"JSON");
					});
							
				} else {	//查無符合條件的記錄
					$("#forRemoveQueryRes").remove();
					tmp = "<div id='forRemoveQueryRes' class='alert'><p>無符合條件的部署記錄. </div>"; 
					$("#queryRes").append(tmp);
				}
			});
			
			$("#queryRes").css("visibility", "visible");
		}).button();
		
		initPage("倒版部署");
		
		if (typeof (JSON) == 'undefined') { //沒有 JSON 時才讓 browser 下載 json2.js
			$('head').append($("<script type='text/javascript' src='js/json2.js'>"));
		}
	}
}).tooltip();
</script>

</head>
<body>
<div id="header"></div>

<p> 
	<form id="q_form">
		<table>
			<tr>
				<th colspan="2" class='center'>--查詢部署記錄--</th>
			</tr>
			<tr>
				<th class='right'>程式名稱：</th>
				<td><input type="text" id="q_fileName" name="q_fileName" ></td>
			</tr>
			<tr>
				<th class='right'>執行人員：</th>
				<td><input type="text" id="q_performer" name="q_performer" size="6" ></td>
			</tr>
			<tr>
				<th class='right'>子系統：</th>
				<td><select id="envSubSysId"><option value="">請選擇</option></select></td>
			</tr>
			<tr>
				<th class='right'>環境：</th>
				<td><select id="envId"><option value="">請選擇</option></select></td>
			</tr>	
			<tr>
				<th class='right'>交付代碼：</th>
				<td><input type="text" id="q_deliverySN" name="q_deliverySN" class="sn"></td>
			</tr>
			
			<tr>
				<th class='right'>資源群組：</th>
				<td><input type="text" id="q_squadid" name="q_squadid" ></td>
			</tr>
			
			<tr>
				<th class='right'>部署計畫代碼：</th>
				<td><input type="text" id="q_deplmainSN" name="q_deplmainSN" class="sn"></td>
			</tr>
			
			<tr>
				<th rowspan="2" class="right">部署日期：</th>
				<td>起：<input type="text" class="date" name="q_fromDate" id="q_fromDate" /></td>
			</tr>
			<tr>
				<td>迄：<input type="text" class="date" name="q_toDate" id="q_toDate" /></td>
			</tr>
			<tr><td class="warning" id="mes" colspan="2">※ 預設查詢近30天記錄</td></tr>
			<tr>
				<td colspan="2" class='center'><button type="button" id="clearBut" >清除</button><button type="button" id="submitQueryBut" >送出</button></td>
			</tr>
		</table>
		<input type="hidden" id="q_envSubSysId" name="q_envSubSysId" />
		<input type="hidden" id="q_envId" name="q_envId" />
		<input type="hidden" id="isSU" name="isSU" />
		<input type="hidden" id="userId" name="userId" />
		<input type="hidden" id="q_performerId" name="q_performerId" />
	</form>
	
	<div id="queryRes" class="dialog">
		
	</div>
	
	<div id="perfromerPicker" class="dialog" title="人員選取">
		<table>
			<tr>
				<th></th>
				<th>#</th>
				<th>姓名</th>
			</tr>
			<tbody id="performerList"></tbody>
		</table>
	</div>
	
	<div id="reDepConfirmBatch" class="dialog">
		<table border="1">
			<tr><th class="right">部署計畫代碼：</th><td class="english_font"></td></tr>
			<tr><th class="right">子系統：</th><td><select disabled="disabled" id="selSub"></select></td></tr>
			<tr><th class="right">環境：</th><td><select id="selEnv"></select></td></tr>
			<tr><td class="right">部署原因</td><td class="warning">※部署原因必須輸入</td></tr>
			<tr><td colspan="2"><textarea></textarea></td></tr>
			<tr><td colspan="2" id="cons"></td></tr>	<!-- 顯示細項的錯誤訊息用 -->
		</table>
		<input type="hidden" id="depmainSN"  name="depmainSN">
	</div>
	
	<div id="reDepConfirmSingle" class="dialog">
		<table border="1">
			<tr><th class="right">檔案名稱：</th><td class="english_font"></td><th>版次：</th><td class="english_font"><select></select></td><th>資源群組</th><tr>
			<tr><th class="right">子系統：</th><td colspan="3"><select disabled="disabled" id="selSub"></select></td><td><button type="button">改變群組</button></td></tr>
			<tr><th class="right">環境：</th><td colspan="3"><select id="selEnv"></select></td><td id="selSquads" rowspan="6"><ul></ul></td></tr>
			<tr><th class="right">執行方式：</th><td id="depType" colspan="3"><select id="selDepType"></select></td></tr>
			<tr><th class="right">延伸路徑：</th><td id="depPath" colspan="3"><input type="text"></td></tr>
			<tr><th colspan="4">部署原因</th></tr>
			<tr><td colspan="4"><textarea></textarea></td></tr>
			<tr><td colspan="4" class="warning">※部署原因必須輸入</td></tr>
		</table>
	</div>
	
	<input type="hidden" id="filePortfolio" name="filePortfolio"  >	<!-- 這次部署的檔案組合，1為有web和nonweb，2為僅有nonweb，3為僅有web -->
	<input type="hidden" id="ptrs" />
	
	<div class="dialog" id="squadSelect" title="選擇資源群組">
		<table>
			<thead>
				<tr><td colspan="2"><select disabled="disabled" ><option></option></select></td></tr>
				<tr>
					<th><input type="checkbox" id="subsysSquadCheckALL" onClick="chkALL('subsysSquad')"></th>
					<th>資源群組</th>
				</tr>
			</thead>
			<tbody id="squadSelect_tb"></tbody>				
		</table>
	</div>
	
	<div class="dialog" id="confirmDep" title="確認部署"></div>
	
	<div class="dialog" id="loading" ><label for="imgLoading" class="english_font"> Loading...</label><br/><img id='imgLoading' src='pic/ajax-loader.gif'></div>
</body>
</html>